package com.xindaoapp.happypet.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.api.AddClient;
import com.xindaoapp.happypet.utils.CommonUtil;
import com.xindaoapp.happypet.utils.ProgressHUD;

/**
 * 退款退货
 * 
 * @author Administrator
 * 
 */
public class OrderRefundActivity extends UMBaseActivity implements OnClickListener {
    private final String TAG = "OrderRefundActivity";
    private Context mContext;
    private ImageButton back;

    private ImageView type1;
    private ImageView type2;

    private TextView cause;
    private TextView price;
    private EditText shuru;
    private Button ok;

    private int type = -1;// 1:退款 2:退货
    private String reason;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_refund);
        mContext = OrderRefundActivity.this;

        initView();
        initData();
    }

    private void initView() {
        back = (ImageButton) findViewById(R.id.cancel);
        type1 = (ImageView) findViewById(R.id.refund_type1);
        type2 = (ImageView) findViewById(R.id.refund_type2);
        cause = (TextView) findViewById(R.id.refund_cause);
        price = (TextView) findViewById(R.id.refund_price);
        shuru = (EditText) findViewById(R.id.refund_shuru);
        ok = (Button) findViewById(R.id.refund_ok);

        back.setOnClickListener(this);
        type1.setOnClickListener(this);
        type2.setOnClickListener(this);
        cause.setOnClickListener(this);
        ok.setOnClickListener(this);
    }

    private void initData() {
        type1.setSelected(true);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.refund_type1:// 退款类型:退款
                if (type1.isSelected()) {
                    type1.setSelected(false);
                } else {
                    type2.setSelected(false);
                    type1.setSelected(true);
                    type = 1;
                }

                break;
            case R.id.refund_type2:// 退款类型:退货
                if (type2.isSelected()) {
                    type2.setSelected(false);
                } else {
                    type1.setSelected(false);
                    type2.setSelected(true);
                    type = 2;
                }
                break;
            case R.id.refund_cause:// 选择退款原因
                final String[] items = new String[] {"不想买了", "信息填写错误，重新预订", "其他原因"};
                new AlertDialog.Builder(mContext).setTitle("取消原因")
                        .setItems(items, new DialogInterface.OnClickListener() {
                            @SuppressLint("NewApi")
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // TODO Auto-generated method stub
                                switch (which) {
                                    case 0:
                                        reason = items[0];
                                        break;
                                    case 1:
                                        reason = items[1];
                                        break;
                                    case 2:
                                        reason = items[2];
                                        break;
                                }
                                cause.setText(reason);
                                cause.setCompoundDrawables(null, null, null, null);
                            }
                        }).show();

                break;
            case R.id.refund_ok:// 提交申请
                String msg = shuru.getText().toString();
                if (type == -1) {
                    Toast.makeText(mContext, "请选择退款类型,谢谢", Toast.LENGTH_SHORT).show();
                    return;
                }
                sendMsg(msg);
                break;
            case R.id.cancel:
                finish();
                break;
            default:
                break;
        }
    }

    private void sendMsg(String msg) {
        if (CommonUtil.checkNetState(mContext)) {
            RequestParams params = new RequestParams();
            final ProgressHUD progressHUD = ProgressHUD.show(mContext, "正在加载...", true, true, null);
            AddClient.get(null, params, new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, String content) {
                    if (null != progressHUD && progressHUD.isShowing()) {
                        progressHUD.dismiss();
                    }
                    int status = -1;
                    JSONObject obj;
                    try {
                        obj = new JSONObject(content);
                        status = obj.getInt("status");
                        if (status == 1) {
                            Toast.makeText(mContext, "申请退款成功,请等待返还款项", Toast.LENGTH_SHORT).show();
                            finish();
                        } else {
                            Toast.makeText(mContext, "申请退款失败,请重新填写", Toast.LENGTH_SHORT).show();
                            return;
                        }
                    } catch (JSONException e1) {
                        e1.printStackTrace();
                    }
                }

                @Override
                public void onFailure(Throwable error, String content) {
                    if (null != progressHUD && progressHUD.isShowing()) {
                        progressHUD.dismiss();
                    }
                    super.onFailure(error, content);
                }
            });
        } else {
            Toast.makeText(mContext, R.string.pLease_check_network, 0).show();
            return;
        }
    }
}
